<?php

//小站广播模型
class Miniblog_model extends Model {

	function Miniblog_model()
	{
		parent::Model();	
		$this->load->database();
		$this->load->model("comment_model");
		$this->load->model("plugins_model", "pm");
	}
	
	//获取列表
	function listitem($offset = 0, $pagecount = 20, $order = "id", $desc = "desc")
	{
		$items = array();
		$query = $this->db->from("module_miniblog")->limit($pagecount, $offset)->order_by($order, $desc)->get();
		if ($query->num_rows()>0) {
			$items = $query->result_array();
			foreach($items as $k => $list){
				$siteid = $list["siteid"];
				$site = $this->sm->get($siteid);
				if($site) {
					$items[$k]["domain"] = $site["domain"];
					$items[$k]["sitename"] = $site["name"];
					$items[$k]["logo"] = $site["logo"];
				} else {
					$items[$k]["domain"] = $items[$k]["sitename"] = $items[$k]["logo"] = "";
				}
				
				//用户信息
				$userid = $list["userid"];
				$userinfo = $this->am->get($userid);
				$items[$k]["userinfo"] = $userinfo;
			}
		}
		return $items;
	}
	
	//计算总数
	function countitems()
	{
		return $this->db->count_all_results("module_miniblog");
	}
	
	//发表广播
	function add($data)
	{
		$data["addtime"] = time();
		$result = $this->db->insert("module_miniblog", $data);
		$id = $this->db->insert_id();
		return $id;
	}
	
	//获取广播
	function get($siteid)
	{
		$row = array();
		$query = $this->db->from("module_miniblog")->where("siteid", $siteid)->order_by("addtime","desc")->get();
		if($query->num_rows()>0) {
			$row = $query->row_array();
			//评论总数
			$commentcount = $this->comment_model->count_miniblog_lists($siteid, $row["id"]);
			$row["commentcount"] = $commentcount;	
			//用户信息
			$userid = $row["userid"];
			$userinfo = $this->am->get($userid);
			if($userinfo) {
				$row["userinfo"] = $userinfo;
			} else {
				$row["userinfo"]["username"] = "";
			}		
		}
		return $row;
	}
	
	//查看广播
	function show($dataid)
	{
		$row = array();
		$query = $this->db->from("module_miniblog")->where("id", $dataid)->order_by("addtime","desc")->get();
		if($query->num_rows()>0) {
			$row = $query->row_array();
			//小站信息
			$site = $this->sm->get_site($row["siteid"]);
			if ($site) {
				$row["site"] = $site;
			} else {
				$row["site"]["name"] = "";
			}
			//用户信息
			$userid = $row["userid"];
			$userinfo = $this->am->get($userid);
			if($userinfo) {
				$row["userinfo"] = $userinfo;
			} else {
				$row["userinfo"]["username"] = "";
			}
		}
		return $row;
	}
	
	//删除
	function delete($dataid)
	{
		$result = $this->db->delete("module_miniblog", array("id"=>$dataid));
		return $result;
	}
	
	//删除选择
	function delselected($delstr)
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		//删除评论
//		$this->comment_model->forum_delete($id);
		$this->db->where_in("dataid", $delid);
		$this->db->delete("comment");
		
		$this->db->where_in("id", $delid);
		$result = $this->db->delete("module_miniblog");
		return $result;
	}
}

/* End of file */